# encoding: utf-8
import time

import pandas as pd
from functools import wraps
from config import config


def retry_except(func):
    """失败重试"""
    @wraps(func)
    def wrapper(*args, **kwargs):
        for i in range(2):
            try:
                return func(*args, **kwargs)
            except Exception as e:
                print(f'第{i + 1}次操作失败, {e}')
            time.sleep(2)
        else:
            raise RuntimeError('5次操作都是失败了')
    return wrapper


def get_data_dict(cate_type=None):
    """获取数据字典"""
    data_df = pd.read_excel(config.DATA_DICT_PATH, dtype={'code': str})
    if cate_type:
        return data_df[data_df['cate_type'] == cate_type].to_dict(orient='records')
    else:
        return data_df.to_dict(orient='records')


def get_strategy_data():
    strategy_data_df = pd.read_excel(config.STRATEGY_DICT_PATH)
    return strategy_data_df


def read_excel_to_dict(file_path):
    data_df = pd.read_excel(file_path, dtype={'code': str})
    return data_df.to_dict(orient='records')
